package com.zret.oasys.operation.emp;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zret.oasys.dbtools.MD5Util;
import com.zret.oasys.json.entity.MsgJson;

public class ChangePwd implements EmpServletOperation {

	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码格式
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		// json格式化对象
		Gson gson = new Gson();
		// 传递json信息类
		MsgJson msg = new MsgJson();
		// 输出流
		PrintWriter out = response.getWriter();
		// 获取用户名与新密码
		String username = "";
		String newpwd = "";
		if(request.getParameter("username") == null) {
			msg.setCode(404);
			msg.setMsg("用户名为空");
		} else if(request.getParameter("newpwd") == null) {
			msg.setCode(404);
			msg.setMsg("新密码为空");
		} else {
			username = request.getParameter("username");
			newpwd = MD5Util.getEncodeByMd5(request.getParameter("newpwd"));
			boolean flag = empService.changeUserPwd(username, newpwd);
			if(flag) {
				msg.setCode(200);
				msg.setMsg("新密码更改成功!");
			} else {
				msg.setCode(500);
				msg.setMsg("新密码更改失败!");
			}
		}
		String jsonObj = gson.toJson(msg);
		out.print(jsonObj);
		out.close();
	}

}
